package bmi

import (
	"fmt"
	"net/http"
	"strconv"
)

func init() {
	fmt.Println("希望我一直健康")
}

func BMI() {
	fmt.Println("输入身高(m):")
	var height float32
	fmt.Scanln(&height)
	fmt.Println("输入体重(kg):")
	var weight float32
	fmt.Scanln(&weight)
	bmi := weight / (height * height)
	fmt.Println(bmi)

}
func BmiApt(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get("name")

	height := r.URL.Query().Get("height")
	ht, err := strconv.ParseFloat(height, 64)
	if err != nil {
		w.Write([]byte("你的身高参数不正确"))
		return
	}
	weight := r.URL.Query().Get("weight")
	wt, err := strconv.ParseFloat(weight, 64)
	if err != nil {
		w.Write([]byte("你的体重参数不正确"))
		return
	}
	w.Write([]byte("欢迎你" + name + "\n"))
	bmi := wt / (ht * ht)
	switch {
	case bmi < 18.5:
		w.Write([]byte(fmt.Sprintf("你太瘦了%v", bmi)))
	case bmi < 23:
		w.Write([]byte(fmt.Sprintf("你是正常的%v", bmi)))
	case bmi < 30:
		w.Write([]byte(fmt.Sprintf("你偏胖%v", bmi)))
	case bmi < 40:
		w.Write([]byte(fmt.Sprintf("你肥胖%v", bmi)))
		return
	}
}
